package com.iot08.admin.interceptor;

import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @version 1.0
 * @Description: 登录拦截
 * @time: 2020/8/20 8:56
 * @author: Rock
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getServletPath();
        //shiro管理的session
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        SysUser user = (SysUser) session.getAttribute(Const.SESSION_USER);
        if (user != null) {
            return true;
        } else {
            //登陆过滤
            response.sendRedirect(Const.LOGIN);
            return false;
        }
    }

}
